﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using ConstructionBidOnline.Biz;
using ConstructionBidOnline.Common;

public partial class ManageJobCategories : System.Web.UI.Page
{
    #region Types

    public enum LayoutType
    {
        Add,
        Edit,
        Search,
        View
    }

    #endregion

    #region Constants

    private const string PROP_DATASRC_JOBCATEGORY = "__JobCategory::JC";
    private const string PROP_LAYOUT = "__JobCategory::UIL";
    private const string PROP_SELECTED_JOBCATEGORY_ID = "__JobCategory::SJCID";

    #endregion

    #region Public Properties

    public JobCategoryCollection DataSourceJobCategory
    {
        get { return (SessionMgr.Session[PROP_DATASRC_JOBCATEGORY] is JobCategoryCollection ? (JobCategoryCollection)SessionMgr.Session[PROP_DATASRC_JOBCATEGORY] : (JobCategoryCollection)(SessionMgr.Session[PROP_DATASRC_JOBCATEGORY] = new JobCategoryCollection())); }
        set { SessionMgr.Session[PROP_DATASRC_JOBCATEGORY] = value; }
    }

    public int SelectedJobCategoryID
    {
        get { return (SessionMgr.Session[PROP_SELECTED_JOBCATEGORY_ID] is int ? (int)SessionMgr.Session[PROP_SELECTED_JOBCATEGORY_ID] : 0); }
        set { SessionMgr.Session[PROP_SELECTED_JOBCATEGORY_ID] = value; }
    }


    public LayoutType UILayout
    {
        get { return (SessionMgr.Session[PROP_LAYOUT] is LayoutType ? (LayoutType)SessionMgr.Session[PROP_LAYOUT] : LayoutType.Search); }
        set { SessionMgr.Session[PROP_LAYOUT] = value; }
    }


    #endregion

    #region Event Handlers

    #region Page Load

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Cache.SetCacheability(HttpCacheability.NoCache);

        if (!Page.IsPostBack)
        {
            DataSourceJobCategory = JobCategory.GetAllJobCategories();
            JobCategoryList1.DataBind(DataSourceJobCategory);
        }
    }

    #endregion

    #region Control: JobCategoryList

    protected void ucJobCategoryList_EditRequested(object sender, Controls_JobCategoryList.EditRequestedEventArgs evt)
    {
        SelectedJobCategoryID = evt.JC.JobCategoryID;
        JobCategoryViewEdit1.DataBind(evt.JC);
        SetLayout(LayoutType.Edit);
    }


    protected void ucJobCategoryList_AddNewRequested(object sender, EventArgs evt)
    {
        JobCategoryViewEdit1.CancelEdit();
        JobCategory newJobCategory = JobCategory.CreateNew(DataSourceJobCategory);
        SelectedJobCategoryID = newJobCategory.JobCategoryID;
        JobCategoryViewEdit1.DataBind(DataSourceJobCategory.FindByJobCategoryID(SelectedJobCategoryID));
        SetLayout(LayoutType.Edit);
    }


    protected void ucJobCategoryList_DataSourceRequested(object sender, EventArgs evt)
    {
        DataSourceJobCategory = JobCategory.GetAllJobCategories();
        JobCategoryList1.DataSource = DataSourceJobCategory;
    }

    #endregion

    #region Control: JobCategoryViewEdit

    protected void ucJobCategoryViewEdit_DataSourceUserRequested(object sender, EventArgs evt)
    {
        JobCategoryViewEdit1.DataSourceJobCategory = DataSourceJobCategory.FindByJobCategoryID(SelectedJobCategoryID);
    }

    #endregion

    #region Save / Cancel Buttons

    protected void btnCancel_Click(object sender, ImageClickEventArgs e)
    {
        JobCategoryViewEdit1.CancelEdit();
        // If we were adding a new jobcategory...then remove the incomplete/not added new jobcategory object from the collection.
        if (DataSourceJobCategory.FindByJobCategoryID(SelectedJobCategoryID).ObjState == BOBase.ObjectState.Added)
        {
            try
            {
                DataSourceJobCategory.Remove(DataSourceJobCategory.FindByJobCategoryID(SelectedJobCategoryID));
                SelectedJobCategoryID = (DataSourceJobCategory[0] != null ? DataSourceJobCategory[0].JobCategoryID : 0);
            }
            catch
            {
            }
        }
        JobCategoryList1.DataBind(DataSourceJobCategory);
        SetLayout(LayoutType.View);
    }
    protected void btnsave_Click(object sender, ImageClickEventArgs e)
    {
        if (JobCategoryViewEdit1.EndEdit())
        {
            JobCategory obj = DataSourceJobCategory.FindByJobCategoryID(SelectedJobCategoryID);
            JobCategory.Update(obj);


            // TODO: Set Feedback text...

            JobCategoryList1.DataBind(DataSourceJobCategory);
            SetLayout(LayoutType.View);

        }
    }


    #endregion

    #endregion

    #region Private

    #region SetLayout

    /// <summary>
    /// Configures the UI for the specified 'uiLayoutType'.
    /// </summary>
    protected void SetLayout(LayoutType uiLayoutType)
    {
        switch (uiLayoutType)
        {
            case LayoutType.Add:
            case LayoutType.Edit:

                pnlJobCategoryList.Visible = false;
                pnlJobCategoryViewEdit.Visible = true;
                pnlButtons.Visible = true;
                break;

            case LayoutType.View:
            case LayoutType.Search:

                pnlJobCategoryList.Visible = true;
                pnlJobCategoryViewEdit.Visible = false;
                pnlButtons.Visible = false;
                break;

            default:
                throw new NotImplementedException("The specified LayoutType is not supported in this release.");
        }

        //SetInitialFocus(uiLayoutType);
    }

    #endregion

    #endregion

}
